package com.bbva.kyfb.kyfbjpatlsb.dominiotlsb;

import java.math.BigDecimal;
import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Iterator;
import java.util.List;
import java.util.Random;

import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;

import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.roo.addon.dod.RooDataOnDemand;
import org.springframework.stereotype.Component;

/**
 * The Class TbdbzbitDataOnDemand.
 */
@Configurable
@Component
@RooDataOnDemand(entity = Tbdbzbit.class)
public class TbdbzbitDataOnDemand {

	/** The rnd. */
	private Random rnd = new SecureRandom();

	/** The data. */
	private List<Tbdbzbit> data;

	/**
	 * Gets the new transient tbdbzbit.
	 *
	 * @param index the index
	 * @return the new transient tbdbzbit
	 */
	public Tbdbzbit getNewTransientTbdbzbit(int index) {
        Tbdbzbit obj = new Tbdbzbit();
        setEmbeddedIdClass(obj, index);
        setAudFmodific(obj, index);
        setAudUsuario(obj, index);
        setCodPid(obj, index);
        setFecRecepcio(obj, index);
        setHmsRecepcio(obj, index);
        setTimFirma(obj, index);
        return obj;
    }

	/**
	 * Sets the embedded id class.
	 *
	 * @param obj the obj
	 * @param index the index
	 */
	public void setEmbeddedIdClass(Tbdbzbit obj, int index) {
		TbdbzbitPK embeddedIdClass;
		if(String.valueOf(index).length() > 8){
			embeddedIdClass = new TbdbzbitPK(new Integer(index).shortValue(), new Integer(index).shortValue(), "codLogonapl_" + String.valueOf(index).substring(0,8), new Character('N'), BigDecimal.valueOf(index));
		}else{
			embeddedIdClass = new TbdbzbitPK(new Integer(index).shortValue(), new Integer(index).shortValue(), "codLogonapl_" + index, new Character('N'), BigDecimal.valueOf(index));
		}
        obj.setId(embeddedIdClass);
    }

	/**
	 * Sets the aud fmodific.
	 *
	 * @param obj the obj
	 * @param index the index
	 */
	public void setAudFmodific(Tbdbzbit obj, int index) {
        Date audFmodific = new GregorianCalendar(Calendar.getInstance().get(Calendar.YEAR), Calendar.getInstance().get(Calendar.MONTH), Calendar.getInstance().get(Calendar.DAY_OF_MONTH), Calendar.getInstance().get(Calendar.HOUR_OF_DAY), Calendar.getInstance().get(Calendar.MINUTE), Calendar.getInstance().get(Calendar.SECOND) + new Double(Math.random() * 1000).intValue()).getTime();
        obj.setAudFmodific(audFmodific);
    }

	/**
	 * Sets the aud usuario.
	 *
	 * @param obj the obj
	 * @param index the index
	 */
	public void setAudUsuario(Tbdbzbit obj, int index) {
        String audUsuario = "audUsu_" + index;
        if (audUsuario.length() > 8) {
            audUsuario = audUsuario.substring(0, 8);
        }
        obj.setAudUsuario(audUsuario);
    }

	/**
	 * Sets the cod pid.
	 *
	 * @param obj the obj
	 * @param index the index
	 */
	public void setCodPid(Tbdbzbit obj, int index) {
        Short codPid = new Integer(index).shortValue();
        obj.setCodPid(codPid);
    }

	/**
	 * Sets the fec recepcio.
	 *
	 * @param obj the obj
	 * @param index the index
	 */
	public void setFecRecepcio(Tbdbzbit obj, int index) {
        Date fecRecepcio = new GregorianCalendar(Calendar.getInstance().get(Calendar.YEAR), Calendar.getInstance().get(Calendar.MONTH), Calendar.getInstance().get(Calendar.DAY_OF_MONTH), Calendar.getInstance().get(Calendar.HOUR_OF_DAY), Calendar.getInstance().get(Calendar.MINUTE), Calendar.getInstance().get(Calendar.SECOND) + new Double(Math.random() * 1000).intValue()).getTime();
        obj.setFecRecepcio(fecRecepcio);
    }

	/**
	 * Sets the hms recepcio.
	 *
	 * @param obj the obj
	 * @param index the index
	 */
	public void setHmsRecepcio(Tbdbzbit obj, int index) {
        Date hmsRecepcio = new GregorianCalendar(Calendar.getInstance().get(Calendar.YEAR), Calendar.getInstance().get(Calendar.MONTH), Calendar.getInstance().get(Calendar.DAY_OF_MONTH), Calendar.getInstance().get(Calendar.HOUR_OF_DAY), Calendar.getInstance().get(Calendar.MINUTE), Calendar.getInstance().get(Calendar.SECOND) + new Double(Math.random() * 1000).intValue()).getTime();
        obj.setHmsRecepcio(hmsRecepcio);
    }

	/**
	 * Sets the tim firma.
	 *
	 * @param obj the obj
	 * @param index the index
	 */
	public void setTimFirma(Tbdbzbit obj, int index) {
        Date timFirma = new GregorianCalendar(Calendar.getInstance().get(Calendar.YEAR), Calendar.getInstance().get(Calendar.MONTH), Calendar.getInstance().get(Calendar.DAY_OF_MONTH), Calendar.getInstance().get(Calendar.HOUR_OF_DAY), Calendar.getInstance().get(Calendar.MINUTE), Calendar.getInstance().get(Calendar.SECOND) + new Double(Math.random() * 1000).intValue()).getTime();
        obj.setTimFirma(timFirma);
    }

	/**
	 * Gets the specific tbdbzbit.
	 *
	 * @param index the index
	 * @return the specific tbdbzbit
	 */
	public Tbdbzbit getSpecificTbdbzbit(int index) {
        init();
        if (index < 0) index = 0;
        if (index > (data.size() - 1)) index = data.size() - 1;
        Tbdbzbit obj = data.get(index);
        return Tbdbzbit.findTbdbzbit(obj.getId());
    }

	/**
	 * Gets the random tbdbzbit.
	 *
	 * @return the random tbdbzbit
	 */
	public Tbdbzbit getRandomTbdbzbit() {
        init();
        Tbdbzbit obj = data.get(rnd.nextInt(data.size()));
        return Tbdbzbit.findTbdbzbit(obj.getId());
    }

	/**
	 * Modify tbdbzbit.
	 *
	 * @param obj the obj
	 * @return true, if successful
	 */
	public boolean modifyTbdbzbit(Tbdbzbit obj) {
        return false;
    }

	/**
	 * Inits the.
	 */
	public void init() {
        data = Tbdbzbit.findTbdbzbitEntries(0, 10);
        if (data == null) throw new IllegalStateException("Find entries implementation for 'Tbdbzbit' illegally returned null");
        if (!data.isEmpty()) {
            return;
        }
        
        data = new ArrayList<com.bbva.kyfb.kyfbjpatlsb.dominiotlsb.Tbdbzbit>();
        for (int i = 0; i < 10; i++) {
            Tbdbzbit obj = getNewTransientTbdbzbit(i);
            try {
                obj.persist();
            } catch (ConstraintViolationException e) {
                StringBuilder msg = new StringBuilder();
                for (Iterator<ConstraintViolation<?>> it = e.getConstraintViolations().iterator(); it.hasNext();) {
                    ConstraintViolation<?> cv = it.next();
                    msg.append("[").append(cv.getConstraintDescriptor()).append(":").append(cv.getMessage()).append("=").append(cv.getInvalidValue()).append("]");
                }
                throw new RuntimeException(msg.toString(), e);
            }
            obj.flush();
            data.add(obj);
        }
    }
}
